// pages/payment/payment.js
const orderService = require('../../../../api/orderService.js')
const advanceSellService = require('../../../../api/advanceSellService.js')
const wxbarcode = require('../../../../utils/self.js');
const util = require('../../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    showCode: false,
    codeText: '',
    codeWidth: 535,
    codeHeight: 130,
    marginTop: '554rpx',
    type: "",
    deliveryTime: '',
    orderStatus: '',
    button: false
  },
  // 返回首页
  toIndex: function () {
    this.setData({
      button: true
    })
    wx.switchTab({
      url: '../../index/index/index'
    })
  },
  // 查看订单详情
  toOrderDetails: function () {
    wx.navigateTo({
      url: '../../order/orderDetails/orderDetails?orderId=' + this.data.orderId
    })
  },

  codeTextPartition: function (val) {
    console.log(val)
    val = val.replace(/\s/g, '').replace(/(\w{4})(?=\w)/g, "$1 ");
    console.log(val)
    return val;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    console.log(options)
    const that = this
    if (options) {
      that.setData({
        orderId: options.orderId,
        codeText: options.orderId
      })
      orderService.getDetailsById(options.orderId).then(res => {
        console.log(res)
        if (res.shipmentType === "SELF" && res.status === 'PAID' || (res.shipmentType === "SELF" && res.status === 'PENDING')) {
          // 待自提订单和已付款订单
          that.setData({
            showCode: false,
            marginTop: '420rpx',
            orderStatus: res.status
          })
          wxbarcode.barcode('barcode', this.data.codeText, this.data.codeWidth, this.data.codeHeight);
          this.setData({
            codeText: this.codeTextPartition(this.data.codeText)
          })
        } else if (res.shipmentType === "SELF" && res.status === 'WAITPAYBALANCE') {
          // 待付尾款订单 自提
          that.setData({
            marginTop: '160rpx',
            type: 'SELF',
            orderStatus: res.status
          })
          that.getAdvanceSellDetails(res)
        } else if (res.shipmentType === "EXPRESS" && res.status === 'WAITPAYBALANCE') {
          // 待付尾款订单 配送
          that.setData({
            marginTop: '160rpx',
            type: 'EXPRESS',
            orderStatus: res.status
          })
          that.getAdvanceSellDetails(res)
        } else if (res.shipmentType === "EXPRESS" && res.business === 'ADVANCE_SELL' && res.status === 'PAID') {
          // 待付尾款订单 配送
          that.setData({
            marginTop: '160rpx',
            type: 'EXPRESS',
            orderStatus: res.status
          })
          that.getAdvanceSellDetails(res)
        } else {
          that.setData({
            orderStatus: res.status
          })
        }
      })
    }
  },
  getAdvanceSellDetails: function (order) {
    const that = this
    const advanceId = order.advanceId
    const goodsList = order.products
    let productId = null
    if (goodsList.length > 0) {
      productId = goodsList[0].productId
    }
    advanceSellService.getByIds(advanceId, productId).then(res => {
      console.log(res)
      that.setData({
        deliveryTime: res.deliveryTime,
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("1")
    if (!this.data.button) {
      // 不是从按钮返回
      console.log("2")
      if (this.options.from === 'shopCart') {
        console.log("3")
        wx.switchTab({
          url: '../../index/index/index'
        })
      } else {
        console.log("4")
        wx.navigateBack({
          delta: 2
        })
      }
    }else{
      wx.switchTab({
        url: '../../index/index/index'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})